package extra01GUI;

import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class VRectangulo extends JDialog implements ActionListener {
	
	private JComboBox cbLista;
	private JLabel lblLadoA;
	private JLabel lblLadoB;
	private JTextField txtLadoA;
	private JTextField txtLadoB;
	private JButton btnCalcular;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public VRectangulo(JFrame F){
		super(F,"Rectangulo");
		
		cbLista = new JComboBox(new String[]{"Rectangulo","Cuadrado"});
		cbLista.addActionListener(this);
		
		btnCalcular = new JButton("Calcular");
		btnCalcular.addActionListener(this);
		
		lblLadoA = new JLabel("Lado A:");		
		txtLadoA = new JTextField(20);
		lblLadoB = new JLabel("Lado B:");		
		txtLadoB = new JTextField(20);
		
		
		FlowLayout fl = new FlowLayout(FlowLayout.RIGHT);
		JPanel pa = new JPanel(fl);
		JPanel pb = new JPanel(fl);
		JPanel pc = new JPanel(fl);		
		
		pa.add(lblLadoA);
		pa.add(txtLadoA);
		
		pb.add(lblLadoB);
		pb.add(txtLadoB);
		
		pc.add(cbLista);
		pc.add(btnCalcular);
		
		setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
		add(pc);
		add(pa);
		add(pb);
		
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		Point punto = F.getLocation();
		setLocation(punto.x, punto.y);
	}
	
	public void Iniciar(){
		pack();
		setResizable(false);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cbLista){
			switch (cbLista.getSelectedIndex()) {
			case 0:
				lblLadoB.setVisible(true);
				txtLadoB.setVisible(true);
				break;
			case 1:
				lblLadoB.setVisible(false);
				txtLadoB.setVisible(false);
				break;			
			default:
				break;
			}
			pack();
		}
		
		if(e.getSource() == btnCalcular){
			MostrarInfo(cbLista.getSelectedIndex());
		}
	}
	
	private void MostrarInfo(int opc){
		switch (opc) {
		case 0:
			InfoRectangulo();
			break;
		case 1:
			InfoCuadrado();
			break;			
		default:
			break;
		}
	}
	
	private void InfoRectangulo(){
		double a,b;
		boolean estado = true;
		
		try{
			a = Double.parseDouble(txtLadoA.getText());
		}catch(NumberFormatException e){
			a = 0;
			estado=false;
		}
		
		try{
			b = Double.parseDouble(txtLadoB.getText());
		}catch(NumberFormatException e){
			b = 0;
			estado=false;
		}
		
		if(estado){
			JOptionPane.showMessageDialog(this, UtilidadGeometria.Rectangulo(new double[] {a,b}), "Resultado", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(this,"Error en el ingreso de los datos", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void InfoCuadrado(){
		double a;
		boolean estado = true;
		
		try{
			a = Double.parseDouble(txtLadoA.getText());
		}catch(NumberFormatException e){
			a = 0;
			estado=false;
		}		
		
		if(estado){
			JOptionPane.showMessageDialog(this, UtilidadGeometria.Rectangulo(new double[] {a}), "Resultado", JOptionPane.INFORMATION_MESSAGE);
		}else{
			JOptionPane.showMessageDialog(this,"Error en el ingreso de los datos", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}
